var CryptoJS = require('crypto-js');
var key = 'liuyunqiang@lx100$#365#$' //加密秘钥  
var iv = '01234567' //偏移量 

// 原文:https://ask.dcloud.net.cn/article/36614

module.exports = {

	/**  
	 * 加密  
	 */
	encryptByDES: function(message) {
		const keyHex = CryptoJS.enc.Utf8.parse(key);
		const encrypted = CryptoJS.TripleDES.encrypt(message, keyHex, {
			iv: CryptoJS.enc.Utf8.parse(iv),
			mode: CryptoJS.mode.CBC,
			padding: CryptoJS.pad.Pkcs7
		});
		return encrypted.toString();
	},

	/**  
	 * 解密  
	 */
	decryptByDES: function(ciphertext) {
		const keyHex = CryptoJS.enc.Utf8.parse(key);
		// direct decrypt ciphertext  
		const decrypted = CryptoJS.TripleDES.decrypt({
			ciphertext: CryptoJS.enc.Base64.parse(ciphertext)
		}, keyHex, {
			iv: CryptoJS.enc.Utf8.parse(iv),
			mode: CryptoJS.mode.CBC,
			padding: CryptoJS.pad.Pkcs7
		});
		return decrypted.toString(CryptoJS.enc.Utf8);
	}
}